package com.gitee.qdbp.jdbc.stream;

import org.springframework.jdbc.core.RowMapper;
import com.gitee.qdbp.able.jdbc.paging.PageList;
import com.gitee.qdbp.able.jdbc.paging.Paging;
import com.gitee.qdbp.jdbc.api.SqlDao;

/**
 * 同时设置queryId/countId, 分页+结果转换后续操作, 只有list()
 *
 * @author zhaohuihua
 * @version 20210530
 */
public class SqlOnIdPairPagingResultStream<T> {

    protected final SqlDao dao;
    protected final String queryId;
    protected final String countId;
    protected final Object params;
    protected final Paging paging;

    protected Class<T> resultType;
    protected RowMapper<T> rowMapper;

    SqlOnIdPairPagingResultStream(SqlDao dao, String queryId, String countId, Object params, Paging paging,
            Class<T> resultType) {
        this.dao = dao;
        this.queryId = queryId;
        this.countId = countId;
        this.params = params;
        this.paging = paging;
        this.resultType = resultType;
    }

    SqlOnIdPairPagingResultStream(SqlDao dao, String queryId, String countId, Object params, Paging paging,
            RowMapper<T> rowMapper) {
        this.dao = dao;
        this.queryId = queryId;
        this.countId = countId;
        this.params = params;
        this.paging = paging;
        this.rowMapper = rowMapper;
    }

    /**
     * 查询列表<br>
     * <pre>
    List&lt;SysUser&gt; users = qdbcBoot.sqlStream()
        .sqlId("SysUserMapper:queryRoleUsers", "SysUserMapper:countRoleUsers")
        .params(map)
        .pageBy(1,10)
        .resultAs(SysUser.class)
        .list().asPartList(); // 分页后返回的是PageList, 需要转换为普通List
     * </pre>
     * 
     * @return 查询结果列表
     */
    public PageList<T> list() {
        if (resultType != null) {
            return this.dao.pageForObjects(queryId, countId, params, paging, resultType);
        } else {
            return this.dao.pageForObjects(queryId, countId, params, paging, rowMapper);
        }
    }

}
